import java.util.*;

/**
 * @author hewei
 * @version 1.0
 * @description: 1156. 单字符重复子串的最大长度
 * @date 2022/11/22 16:09
 */

public class MaxRepOpt1 {

    public static void main(String[] args) {
        System.out.println(new MaxRepOpt1().maxRepOpt1("aabbaba"));
    }

    public int maxRepOpt11(String text) {
        int[] count = new int[26];
        char[] chars = text.toCharArray();
        int n = text.length();
        for (int i = 0; i < n; i++) {
            char c = text.charAt(i);
            count[c - 'a']++;
        }
        HashMap<Character, Integer> map = new HashMap<>();
        map.put(text.charAt(0), map.getOrDefault(text.charAt(0), 0) + 1);
        int l = 0, r = 1, ans = 1, cnt = 1;
        int flag = 1;
        count[chars[0] - 'a']--;
        while (r < n) {
            Integer a = map.getOrDefault(chars[r], 0);
            if (a == 0) {
                ++flag;
            } else if (a == 1) {
                --flag;
            }
            --count[chars[r] - 'a'];
            map.put(chars[r++], a + 1);
            if (map.size() > 2 || (map.size() == 2 && flag == 0)) {
                map.put(chars[l], map.get(chars[l]) - 1);
                Integer temp = map.get(chars[l]);
                if (temp == 0) {
                    map.remove(chars[l]);
                    --flag;
                } else if (temp == 1) {
                    ++flag;
                }
                count[chars[l] - 'a']++;
                ++l;
            } else {
                map.size();
                List<Character> list = new ArrayList<>();
                for (Map.Entry<Character, Integer> entry : map.entrySet()) {
                    list.add(entry.getKey());
                }
                if (list.size() == 2) {
                    char key1 = list.get(0), key2 = list.get(1);
                    if (Objects.equals(map.get(key1), map.get(key2))) {
                        if (count[key1 - 'a'] > 0 || count[key2 - 'a'] > 0) {
                            ans = r - l;
                        } else {
                            map.put(chars[l], map.get(chars[l]) - 1);
                            Integer temp = map.get(chars[l]);
                            if (temp == 0) {
                                map.remove(chars[l]);
                                --flag;
                            } else if (temp == 1) {
                                ++flag;
                            }
                            count[chars[l] - 'a']++;
                            ++l;
                        }
                    } else {
                        int value1 = map.get(key1), value2 = map.get(key2);
                        if (value1 > value2) {
                            if (count[key1 - 'a'] > 0) {
                                ans = r - l;
                            } else {
                                map.put(chars[l], map.get(chars[l]) - 1);
                                Integer temp = map.get(chars[l]);
                                if (temp == 0) {
                                    map.remove(chars[l]);
                                    --flag;
                                } else if (temp == 1) {
                                    ++flag;
                                }
                                count[chars[l] - 'a']++;
                                ++l;
                            }
                        } else {
                            if (count[key2 - 'a'] > 0) {
                                ans = r - l;
                            } else {
                                map.put(chars[l], map.get(chars[l]) - 1);
                                Integer temp = map.get(chars[l]);
                                if (temp == 0) {
                                    map.remove(chars[l]);
                                    --flag;
                                } else if (temp == 1) {
                                    ++flag;
                                }
                                count[chars[l] - 'a']++;
                                ++l;
                            }
                        }
                    }
                } else {
                    ans = r - l;
                }
            }
        }
        return ans;
    }

    public int maxRepOpt1(String text) {
        char[] chars = text.toCharArray();
        int[] count = new int[26];
        int n = chars.length, cnt = 1, ans = 1;
        for (char c : chars) {
            count[c - 'a']++;
        }
        char last = chars[0];
        for (int i = 1; i < n; i++) {
            if (chars[i] == last) {
                cnt++;
            } else {
                int index = i + 1;
                while (index < n && chars[index] == last) {
                    ++cnt;
                    ++index;
                }
                if (count[last - 'a'] > cnt) ++cnt;
                ans = Math.max(ans, cnt);
                cnt = 1;
                last = chars[i];
            }
        }
        if (count[last - 'a'] > cnt) ++cnt;
        ans = Math.max(ans, cnt);
        return ans;
    }
}
